package cn.dm;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 并发编程核心回顾
 * 自己可以点到具体类中看源码
 */
public class Review {


    public static void main(String[] args) throws Exception {

        /**
         * ConcurrentMap
         */
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();

        /**
         * CopyOnWrite
         */
        CopyOnWriteArrayList<String> cowal = new CopyOnWriteArrayList<>();
        cowal.add("aaaa");

        /**
         * Atomic系列类
         */
        AtomicLong count = new AtomicLong(1);
        boolean flag = count.compareAndSet(0, 2);
        System.err.println(flag);
        System.err.println(count.get());


        /**
         *  Object锁
         */
        Object lock = new Object();
        Thread ThreadA = new Thread(new Runnable() {
            @Override
            public void run() {
                int sum = 0;
                for (int i = 0; i < 10; i++) {
                    sum += i;
                }
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("object lock sum = " + sum); // 5s后输出结果45
            }
        });
        ThreadA.start();
        Thread.sleep(5000); // 主线程休眠5s
        synchronized (lock) {
            lock.notify();
        }

        /**
         *  LockSupport锁
         */
        Thread ThreadB = new Thread(new Runnable() {
            @Override
            public void run() {
                int sum = 0;
                for (int i = 0; i < 10; i++) {
                    sum += i;
                }
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LockSupport.park(); //后执行
                System.out.println("LockSupport sum: " + sum); // 3s后输出结果45
            }
        });
        ThreadB.start();
        Thread.sleep(3000); // 主线程休眠3s
        LockSupport.unpark(ThreadB); //先执行


        /**
         * 线程池
         */
        Executors.newCachedThreadPool();
        Executors.newFixedThreadPool(10);

        ThreadPoolExecutor pool = new ThreadPoolExecutor(5,
                Runtime.getRuntime().availableProcessors() * 2,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(200),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread t = new Thread(r);
                        t.setName("order-thread");
                        if (t.isDaemon()) {
                            t.setDaemon(false);
                        }
                        if (Thread.NORM_PRIORITY != t.getPriority()) {
                            t.setPriority(Thread.NORM_PRIORITY);
                        }
                        return t;
                    }
                },
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.err.println("拒绝策略:" + r);
                    }
                });
        pool.shutdown(); // 线程池用完之后关闭


        /**
         * 重入锁
         */
        ReentrantLock reentrantLock = new ReentrantLock(true);
    }
}
